<template>
  <div>
    <el-dialog
      :title="title"
      :visible.sync="dialogVisible"
      :close-on-click-modal="false"
      width="800px"
      @closed="resetForm('form')"
    >
      <div>
        <el-form ref="form" :rules="rules" :model="form" label-width="80px">
          <el-form-item label="选择专家" prop="expertsInfoList">
            <div class="user-wrap">
              <p v-show="initialValue.userId.length == 0">请选择人员</p>
              <el-tag
                v-for="tag in initialValue.userId"
                :key="tag.userName"
                closable
                disable-transitions
                @close="handleClose(tag)"
              >
                {{ tag.userName }}
              </el-tag>
            </div>
            <el-button
              class="user-select-button"
              icon="iconfont icon-addrole"
              type="custom"
              plain
              @click="handleSelect()"
            >
              选择人员
            </el-button>
          </el-form-item>
          <el-form-item label="备注">
            <el-input v-model.trim="form.name"></el-input>
          </el-form-item>
        </el-form>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button type="custom" v-preventReClick @click="submitForm('form')"
          >提 交</el-button
        >
        <el-button @click="dialogVisible = false">取 消</el-button>
      </span>
    </el-dialog>
    <UserSelect
      ref="userSelect"
      :initialValue="initialValue"
      @e-userData="setUserId"
    />
  </div>
</template>
<script>
import {
  expertsReviewTeamAdd,
  expertsReviewTeamUpdate,
} from '@/api/admin/audit/expertsReviewTeam.js'
import Bus from '@/utils/bus.js'
export default {
  components: {
    UserSelect: () => import('@/components/userSelect'),
  },
  data() {
    return {
      title: '新增',
      dialogVisible: false,
      addOrEdit: 1,
      checkedData: {},
      form: {
        name: '', //名称
        leader: 'NO', //是否组长，YES,NO
        reviewId: '', //评审会
        expertsInfoList: [], //专家集合
      },
      rules: {
        expertsInfoList: [
          { required: true, message: '请选择专家', trigger: 'blur' },
        ],
      },
      initialValue: {
        userId: [],
      },
    }
  },
  props: ['reviewData'],
  watch: {
    reviewData: {
      immediate: true,
      handler() {
        this.form.reviewId = this.reviewData.id
      },
    },
  },
  mounted() {
    Bus.$on('showDialogReviewTeamAdd', this.init)
  },
  beforeDestroy() {
    Bus.$off('showDialogReviewTeamAdd')
  },
  methods: {
    //显示弹窗
    init(row) {
      if (row) {
        this.form = { ...this.form, ...row }
        this.form.date = [this.form.startTime, this.form.endTime]
        this.$set(this.form, 'id', row.id)
      } else {
        this.form = this.$options.data().form
        this.$delete(this.form, 'id')
      }
      this.title = row ? '编辑' : '新增'
      this.dialogVisible = true
    },
    submitForm(form) {
      this.form.expertsInfoList = this.initialValue.userId
      this.form.reviewId = this.reviewData.id
      this.$refs[form].validate((valid) => {
        if (valid) {
          this.title == '新增' ? this.add() : this.edit()
        }
      })
    },
    setUserId(v) {
      this.initialValue.userId = []
      if (v.hasOwnProperty('userId')) {
        this.initialValue.userId = v.userId
      } else {
        this.initialValue.userId.push(v)
      }
    },
    handleClose(row) {
      this.initialValue.userId = this.initialValue.userId.filter(
        (v) => v.id != row.id,
      )
    },
    //新增
    add() {
      expertsReviewTeamAdd(this.form).then((res) => {
        this.done(res)
      })
    },
    //编辑
    edit() {
      expertsReviewTeamUpdate(this.form).then((res) => {
        this.done(res)
      })
    },
    done(res) {
      if (!res || res.data.code != 0) return
      this.dialogVisible = false
      Bus.$emit('refreshReviewTeam')
      this.$message({
        message: '操作成功',
        type: 'success',
      })
    },
    handleSelect() {
      Bus.$emit('userSelect')
    },
    //重置表单
    resetForm(form) {
      this.$refs[form].resetFields()
      this.initialValue.userId = []
    },
  },
}
</script>
<style scoped>
.user-wrap {
  display: inline-block;
  width: 560px;
  border: 1px solid #dcdfe6;
  border-radius: 5px;
  overflow: hidden;
  min-height: 40px;
  cursor: not-allowed;
}
.user-wrap p {
  line-height: 40px;
  color: #bbb;
  text-indent: 10px;
}
.user-wrap span {
  margin: 0 5px;
}
.user-select-button {
  float: right;
  padding-right: 10px;
  cursor: pointer;
}
.user-select-button span {
  font-size: 28px;
  color: #930b0b;
}
</style>